//
//  AppDelegate+AppDelegateModel.m
//  WKWebVIewDemo
//
//  Created by Yonger on 2018/7/19.
//  Copyright © 2018年 WKWebView. All rights reserved.
//

#import "AppDelegate+AppDelegateModel.h"

#define NetworkDisconnectedTitle        @"网络连接已断开"
#define ConnectedByOtherConnectedTitle  @"已连接互联网"
#define ConnectedBy3GConnectedTitle     @"当前为运营商网络"
#define ConnectedByWifiTitle            @"已连接到本地WiFi"

@implementation AppDelegate (AppDelegateModel)

+(AppDelegate*)shared{
    AppDelegate *appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
    return appDelegate;
}

+(void)connectedByOtherNetworkAlert{
    [self showViewWithTitle:ConnectedByOtherConnectedTitle
                  withImage:[UIImage imageNamed:@"utilsConnect"]
        withBackgroundColor:ColorWithSixteen(0xfb7272, 1)];
}

+(void)connectedBy3GNetworkAlert{
    [self showViewWithTitle:ConnectedBy3GConnectedTitle
                  withImage:[UIImage imageNamed:@"utilsConnect"]
        withBackgroundColor:ColorWithSixteen(0xfc6363, 1)];
}

+(void)connectedByWifiNetworkAlert{
    [self showViewWithTitle:ConnectedByWifiTitle
                  withImage:[UIImage imageNamed:@"utilsConnect"]
        withBackgroundColor:ColorWithSixteen(0xfb7272, 1)];
}

+(void)connectedByNoNetworkAlert{
    [self showViewWithTitle:NetworkDisconnectedTitle
                  withImage:[UIImage imageNamed:@"utilsConnect"]
        withBackgroundColor:ColorWithSixteen(0x999999, 1)];
}

+(UIView*)showViewWithTitle:(NSString*)title withImage:(UIImage*)image withBackgroundColor:(UIColor*)color{
    UIView *view = [[AppDelegate shared].window viewWithTag:9090];
    if (view == nil) {
        view = [[UIView alloc] initWithFrame:CGRectMake(0, -UScreenHeight, UScreenWidth, hNaviBarHeight)];
        [view setTag:9090];
        [[AppDelegate shared].window addSubview:view];
        
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, kStatusBarHeight+(kNaviBarABXHeight - 25)*0.5, 25, 25)];
        [view addSubview:imageView];
        [imageView setTag:111];
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50, imageView.frame.origin.y, UScreenWidth - 60, 25)];
        [label setTextColor:[UIColor whiteColor]];
        [label setFont:[UIFont systemFontOfSize:16]];
        [label setTag:222];
        [view addSubview:label];
    }
    
    [view setBackgroundColor:color];
    
    UIImageView *imageView = [view viewWithTag:111];
    if (imageView) {
        [imageView setImage:image];
    }
    
    UILabel *label = [view viewWithTag:222];
    if (label) {
        [label setText:title];
    }
    
    [self showView];
    
    return view;
}


- (void)networkAlerViewDismiss{
    UIView *view = [[AppDelegate shared].window viewWithTag:9090];
    if (view) {
        [UIView animateWithDuration:0.3
                         animations:^{
                             view.frame = CGRectMake(0, -hNaviBarHeight, UScreenWidth, hNaviBarHeight);
                         }
                         completion:^(BOOL finished) {
                         }];
    }
}

+ (void)showView{
    UIView *view = [[AppDelegate shared].window viewWithTag:9090];
    if (view) {
        view.frame = CGRectMake(0, - hNaviBarHeight, UScreenWidth, hNaviBarHeight);
        [UIView animateWithDuration:0.25
                         animations:^{
                             view.frame = CGRectMake(0, 0, UScreenWidth, hNaviBarHeight);
                         }
                         completion:^(BOOL finished) {
                             [[AppDelegate shared] performSelector:@selector(networkAlerViewDismiss)
                                                                   withObject:nil
                                                                   afterDelay:1.5];
                         }];
    }
}

@end
